import os
import asyncio
import polars as pl

from nautilus_trader.adapters.binance.common.enums import BinanceAccountType
from nautilus_trader.adapters.binance.factories import get_cached_binance_http_client
from nautilus_trader.adapters.binance.futures.providers import BinanceFuturesInstrumentProvider
from nautilus_trader.common.component import LiveClock
from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
from nautilus_trader.adapters.binance.http import market
from nautilus_trader.adapters.binance.http.market import BinanceMarketHttpAPI

clock = LiveClock()
account_type = BinanceAccountType.MARGIN
BINANCE_API_KEY = {}

# key secret 需要去币安申请
# https://www.suitechsui.us/zh-CN/my/settings/api-management
# 申请后添加到环境变量中

async def get_depth(client, symbol, limit=100):
    http = market.BinanceDepthHttp(client=client, base_endpoint="/api/v3/")
    param = market.BinanceDepthHttp.GetParameters(symbol=symbol, limit=limit)

    return await http.get(params=param)

async def main():
    client = get_cached_binance_http_client(
    clock=clock,
    account_type=account_type,
    api_key=os.environ.get("BINANCE_API_KEY"),
    api_secret=os.environ.get("BINANCE_API_SECRET"),
    base_url="https://testnet.binance.vision",
    is_testnet=True,
)
    api = BinanceMarketHttpAPI(client, account_type)
    
    return await api.query_depth(symbol="BTCUSDT", limit=5000)
    
if __name__ == "__main__":
    r = asyncio.run(main())
    print(r)
    df = pl.DataFrame(r)
    df = pl.DataFrame()